package champPackage;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LogInServlet
 */
@WebServlet("/LogInServlet")
public class LogInServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LogInServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String userName= request.getParameter("user");
		String password=request.getParameter("password");
		DBManager manager= DBManager.getInstance();
		try {
			if(manager.isCorrectUser(userName, password)){
				request.getSession().setAttribute("username", userName);
				RequestDispatcher dispatcher= request.getRequestDispatcher("HomeLoggedIn.jsp");
				dispatcher.forward(request, response);
			}else{
				System.out.println("aq shemovida2");
				RequestDispatcher dispatcher= request.getRequestDispatcher("http://localhost:8080/ChampionshipProject/homePage.jsp");
				dispatcher.forward(request, response);
			}
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			System.out.println("logInServlet metodshi manager.isCorrectUser ze amoagdo exeption");
			e.printStackTrace();
		}
		catch(SQLException ex){
			ex.printStackTrace();
		}
	}

}
